package com.bbx.taxi.client.DB;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.bbx.api.sdk.model.comm.returns.line.Cities;
import com.bbx.api.sdk.model.comm.returns.line.To_Cities;
import com.bbx.api.sdk.model.passanger.Token;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.client.DB.Util.DBHelper;
import com.bbx.taxi.client.DB.Util.DBcolumns;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * 线路
 * 
 */

public class LineSnDB implements DBcolumns {
	private DBHelper helper;

	public LineSnDB() {
		MyApplication myApplication=MyApplication.getInstance();
		helper = DBHelper.getInstance(myApplication.getApplicationContext(),null);
	}

	/**
	 * 打开只读的数据库
	 */
	public SQLiteDatabase onOpenRead() {
		if(helper==null){
			helper = DBHelper.getInstance(MyApplication.getInstance().getApplicationContext(), null);
		}
		SQLiteDatabase SQLdb ;
		try{SQLdb= helper.getReadableDatabase();}catch(Exception e){
			helper = DBHelper.getInstance(MyApplication.getInstance().getApplicationContext(), null);
			SQLdb= helper.getReadableDatabase();
		}
		return SQLdb;
	}

	/**
	 * 打开只写的数据库
	 */
	public SQLiteDatabase onOpenWrite() {
		if(helper==null){
			helper = DBHelper.getInstance(MyApplication.getInstance().getApplicationContext(), null);
		}
		SQLiteDatabase SQLdb ;
		try{SQLdb= helper.getReadableDatabase();}catch(Exception e){
			helper = DBHelper.getInstance(MyApplication.getInstance().getApplicationContext(), null);
			SQLdb= helper.getReadableDatabase();
		}
		return SQLdb;
	}

	/**
	 * 查询
	 */
	public Cursor onQuery() {
		SQLiteDatabase SQLdb = onOpenRead();
		Cursor cursor = SQLdb.query(TABLE_LINESN, null, null, null, null, null,
				LINESN_ID + " desc");
		return cursor;

	}

	/**
	 * 根据城市的cn_name[中文]
	 */
	public Cursor onQuery(String cn_name) {
		SQLiteDatabase SQLdb = onOpenRead();
		Cursor cursor = SQLdb.query(TABLE_LINESN, null, LINESN_CN_NAME + "='"
				+ cn_name + "'", null, null, null, LINESN_ID + " desc");
		return cursor;

	}

	/**
	 * 获取可以到达的城市
	 */
	public ArrayList<To_Cities> readToCityDB(String cn_name) {
		Cities cities = null;
		Cursor c = onQuery(cn_name);
		c.moveToFirst();
		if (c.getCount() > 0) {
			do {
				Map<String, String> map = new HashMap<String, String>();
				for (int i = 0; i < c.getColumnCount(); i++) {
					map.put(c.getColumnName(i), c.getString(i));
				}
				cities = (Cities) new JsonBuild().getData(Cities.class,
						map.get(DBcolumns.LINESN_JSON));
			} while (c.moveToNext());
		}
		onColse();
		if(cities==null)return null;
		return cities.getTo_CityList();
	}
	/**
	 * 获取可以到达的城市
	 */
	public ArrayList<Cities> readAllDB() {
		ArrayList<Cities> list=new ArrayList<Cities>();
		Cities cities = null;
		Cursor c = onQuery();
		c.moveToFirst();
		if (c.getCount() > 0) {
			do {
				Map<String, String> map = new HashMap<String, String>();
				for (int i = 0; i < c.getColumnCount(); i++) {
					map.put(c.getColumnName(i), c.getString(i));
				}
				cities = (Cities) new JsonBuild().getData(Cities.class,
						map.get(DBcolumns.LINESN_JSON));
				list.add(cities);
			} while (c.moveToNext());
		}
		onColse();
		return list;
	}

	/**
	 * 删除
	 */
	public void onDelete() {
		SQLiteDatabase SQLdb = onOpenWrite();
		SQLdb.delete(TABLE_LINESN, null, null);
		SQLdb.close();
	}
	/**
	 * 删除
	 */
	public void onDelete(String cn_name) {
		SQLiteDatabase SQLdb = onOpenWrite();
		String[] whereArgs = { String.valueOf(cn_name) };
		int i = SQLdb.delete(TABLE_LINESN, LINESN_CN_NAME + "=?", whereArgs);
		SQLdb.close();
	}
	/**
	 * 插入一条数据
	 */
	public void onWrite(String name, String cn_name, String json) {
		//写入之前，先清空同一个城市
//		onDelete(cn_name);
		SQLiteDatabase SQLdb = onOpenWrite();
		ContentValues values = new ContentValues();
		values.put(LINESN_NAME, name);
		values.put(LINESN_CN_NAME, cn_name);
		values.put(LINESN_JSON, json);
		SQLdb.insert(TABLE_LINESN, LINESN_ID, values);
		SQLdb.close();
	}

	/**
	 * 关闭数据库
	 */
	public void onColse() {
		SQLiteDatabase SQLdb = onOpenWrite();
		SQLdb.close();
	}
}
